const uuidv1 = require('uuid/v1');
const Messages = require('../mongoose/messages');
const Member = require('../mongoose/member');
const Unread = require('../mongoose/unread');
let users={}
module.exports = {
    async receiveMessage(data, connection) {
        let message = JSON.parse(data)
        let { roomId, uuid, other, msg,type,duration} = message
        let messages = await Member.findOne({
            'member.0': {
                $in: [uuid, other]
            },
            'member.1': {
                $in: [uuid, other]
            },
        })
        let time = Date.now()
        if (!roomId) {
            // 没有聊过天，需要创建
            if (!messages) {
                let newRoomId=uuidv1()
                await Member.create({
                    member: [uuid, other],
                    roomId:newRoomId
                })
                await Messages.create({
                    time,
                    uuid,
                    msg,
                    type,
                    roomId:newRoomId
                })
                await Unread.create({
                    unread:[
                        {
                            uuid,
                            time
                        }
                    ],
                    roomId:newRoomId
                })
                message.time = time
                message.roomId = newRoomId
                this.notifyManyPeople([uuid, other],JSON.stringify(message))

                // 聊过天，加上roomId返回
            } else {
                await Messages.create({
                    time,
                    uuid,
                    msg,
                    type,
                    roomId: messages.roomId
                })
                await Unread.findOneAndUpdate({
                    roomId:messages.roomId,'unread.uuid':uuid
                },{'unread.$.time':time})
                message.roomId = messages.roomId
                message.time = time
                this.notifyManyPeople(messages.member,JSON.stringify(message))
            }
            // 有roomId直接相应
        } else {
            await Messages.create({
                time,
                uuid,
                msg,
                type,
                duration,
                roomId: messages.roomId
            })
            // 修改这个用户的未读时间

       let findOneAndUpdate=  await Unread.findOneAndUpdate({
                roomId,'unread.uuid':uuid
            },{'unread.$.time':time})
            // 新增这个用户的未读数据
            if(!findOneAndUpdate)await Unread.findOneAndUpdate({
                roomId
            },{$addToSet:{
               unread :{
                    uuid,time

                }
            }})

            message.time = time
            this.notifyManyPeople(messages.member,JSON.stringify(message))
        }
    },
    addUser(uuid,ctx){
        users[uuid]=ctx
    },
    removeUser(uuid){
        delete users[uuid]
    },
    notifyManyPeople(arr,msg){
        arr.forEach(item=>{
           if(users[item]) users[item].sendUTF(msg)
        })
    }
}